package com.supor.aiot.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/**
 * @author zook
 */
public class ExternalPathUtils {

    public static void jumpShoppe(Activity activity, String url) {
        try {
            if (url.contains("taobao.com")) {
                toTaoBao(activity, url);
            } else if (url.contains("tmall.com")) {
                toTianMao(activity, url);
            } else if (url.contains("jd.com")) {
                toJingDong(activity, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri uri = Uri.parse(url);
            intent.setData(uri);
            activity.startActivity(intent);
        }
    }


    /**
     * 跳转到淘宝
     *
     * @param url 淘宝商品链接地址
     */
    private static void toTaoBao(Activity context, String url) throws Exception {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        intent.setClassName("com.taobao.taobao", "com.taobao.tao.detail.activity.DetailActivity");
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        /**第二种方式
         String id = getUrlParam(url,"id");
         String taobao_url= "taobao://item.taobao.com/item.htm?id="+id+"";
         intent = new Intent(Intent.ACTION_VIEW, Uri.parse(taobao_url));
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent); */
    }

    /**
     * 跳转到天猫
     *
     * @param url 天猫商品链接地址
     */
    private static void toTianMao(Activity context, String url) throws Exception {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        String id = getUrlParam(url, "id");
        String tmall_url = "tmall://tmallclient/?{\"action\":\"item:id=" + id + "\"}";
        Uri uri = Uri.parse(tmall_url);
        intent.setData(uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 跳转到京东
     *
     * @param url 京东商品链接地址
     */
    private static void toJingDong(Activity context, String url) throws Exception {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        String startHtml = url.substring(0, url.indexOf(".html"));
        String id = startHtml.substring(startHtml.lastIndexOf("/") + 1);
        String jd_url = "openapp.jdmobile://virtual?params=%7B%22sourceValue%22:%220_productDetail_97%22,%22des%22:%22productDetail%22,%22skuId%22:%22" + id + "%22,%22category%22:%22jump%22,%22sourceType%22:%22PCUBE_CHANNEL%22%7D";
        //两种url拼接都可以
        //String jd_url = "openApp.jdMobile://virtual?params={\"category\":\"jump\",\"des\":\"productDetail\",\"skuId\":\""+id+"\",\"sourceType\":\"JSHOP_SOURCE_TYPE\",\"sourceValue\":\"JSHOP_SOURCE_VALUE\"}";
        Uri uri = Uri.parse(jd_url);
        intent.setData(uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 获取URL中的某个参数
     */
    public static String getUrlParam(String url, String name) {
        Uri uri = Uri.parse(url);
        return uri.getQueryParameter(name);
    }
}
